
exports.Middleware = (function(){
  var Middleware = function(keyValueStore, sessionStore){
        this.keyValueStore = keyValueStore;
        this.sessionStore = sessionStore;
  };


  Middleware.prototype.checkMobile = function(){
    return function (req, res, next){
      var ua = req.header('user-agent');

      req.isMobile = /mobile/i.test(ua);

      next();
    };
  };



    Middleware.prototype.needAuth = function(){

        var self = this;
        return function(req, res, next)
        {
            if (req.url !== '/login' || req.url !== '/') {
                if (req.session.user) {
                    next();
                }
                else {
                    if(req.url == '/')
                    {
                        next();
                    }
                    else
                    {

                        self.keyValueStore.setAuthRedirectionUrl(req.connection.remoteAddress, req.url);
                        res.redirect('/login');
                    }
                    /**//*   next();*/
                }
            }
            else
            {
                next();
            }
        };
    };


    Middleware.prototype.needs = function(needs){
            var needArray = [];

            for(var i = 0; i < needs.length; ++i){
                switch(needs[i].toLowerCase()){
                    case 'auth':
                        needArray.push(this.needAuth());

                        break;
                    /*     case 'fileid': needArray.push(this.needFileId()); break;
                     case 'profile': needArray.push(this.needProfile()); break;*/
                }
            }


            return needArray;


    };
  return Middleware;
})();